package com.vc.audio;

import com.vc.core.IAudioEventListener;

import android.content.Context;

public class VcAudioManager {

	private AudioMain m_AudioMain;
	public AudioMain getM_AudioMain() {
		return m_AudioMain;
	}
	private IAudioEventListener m_VcCtrl;
	
	public VcAudioManager(IAudioEventListener controller) {
		m_VcCtrl = controller;
	}
	
	public void InitAudio(Context context, boolean bMicphoneMute, int audioMode)
	{
		m_AudioMain = new AudioMain(context);
		m_AudioMain.SetController(m_VcCtrl);
		
		int[] mSamplerate = new int[1];
		int[] mFramelen = new int[1];
		int[] mFormat = new int[1];
		int[] mWorkmode = new int[1];
		if(m_VcCtrl != null) {      
			m_VcCtrl.getAudioDeviceInfo(mFormat, mSamplerate, mFramelen,mWorkmode);           
        }
		
		if(m_AudioMain != null) {
			m_AudioMain.Init(mSamplerate[0],mFramelen[0],1,mWorkmode[0]);
			int curVol = m_AudioMain.GetPlayoutVolume(audioMode);
			int maxVol = m_AudioMain.GetMaxPlayoutVolume(audioMode);
			m_AudioMain.EnableMicrophoneMute(bMicphoneMute);
			m_AudioMain.SetPlayoutSpeaker(!(0 == curVol));
			m_AudioMain.SetPlayoutSpeaker(!m_AudioMain.isHeadsetPluged());

			if (maxVol != 0) {
				int setVol = 255 * curVol / maxVol;
				m_AudioMain.SetPlayoutVolume(setVol, audioMode);
			}
		}
	}
	
	public void StartAudio() {
		if(m_AudioMain != null) {
			m_AudioMain.StartSound();
		}
	}
	
	public void StopAudio() {
		if(m_AudioMain != null)
		{
			m_AudioMain.StopSound();
			m_AudioMain.Uninit();
		}
	}
	
	public int setAudioInputMute(boolean enable) {
		if(m_AudioMain != null) {
			return m_AudioMain.setAudioInputMute(enable);
		}
		return -1;
	}
	
	public int setAudioOutputMute(boolean enable) {
		if(m_AudioMain != null) {
			return m_AudioMain.setAudioOutputMute(enable);
		}
		return -1;
	}
	
	public int setAudioVolume(int volume) {
		if (m_VcCtrl == null)
			return -1;

		//return m_VcCtrl.setAudioSpeakerVolume(volume);
		if(null != m_AudioMain)
		   m_AudioMain.SetPlayoutVolume(volume);
		
		return 0;
	}
	public int getAudioVolumeMode(){

		if(null != m_AudioMain)
			return m_AudioMain.getVolumeMode();

		return 0;

	}
}
